# -*- coding: utf-8 -*-
'''
Copyright(C) 2013 "Kevin Xu <ssonxu@gmail.com>"

This file is part of acqu-center.

acqu-center is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

acqu-center is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with acqu-center.  If not, see <http://www.gnu.org/licenses/>.
'''
#
#--------------------------------------------------------------------
# Data Structures
#--------------------------------------------------------------------
# @email: { 
#     xmpp: @gtalk,
#     facebook: @facebook,
#     twitter: @twitter,
#     emails:[@email1, @email2, ...],
#     labels: [@label1, @label2, ...],
# }
#

from Predef import Predef as PRE
from Predef import EmailProperty
from box import Box 
from google.appengine.ext import ndb
from google.appengine.api import xmpp

class Contact(ndb.Model):
    ############
    # Initialize
    ############
    xmpp = EmailProperty(required=True)
    facebook = EmailProperty()
    twitter = EmailProperty()
    emails = EmailProperty(repeated=True)

    subs = ndb.IntegerProperty(repeated=True, indexed=False)

    def clearSubs(self):
        self.subs=[]

    @classmethod
    def listUsers(cls):
        return cls.query().fetch()

    @staticmethod
    def addUser(x):
        u = Contact.getCon(x) 
        if u is None:
            c = Contact(xmpp=x)
            c.put()
            xmpp.send_invite(x)
            return c
        else:
            return u

    @classmethod
    def getCon(cls, x):
        query = cls.query(cls.xmpp == x)
        return query.get()

